"""
应用配置管理
"""

from pydantic_settings import BaseSettings
from typing import Optional
import os


class Settings(BaseSettings):
    """应用配置类"""
    
    # 数据库配置
    DATABASE_URL: str
    DB_HOST: str = "localhost"
    DB_PORT: int = 3306
    DB_USER: str
    DB_PASSWORD: str
    DB_NAME: str
    
    # Redis配置
    REDIS_URL: str
    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    REDIS_DB: int = 0
    REDIS_PASSWORD: Optional[str] = None
    
    # Celery配置
    CELERY_BROKER_URL: str
    CELERY_RESULT_BACKEND: str
    
    # JWT配置
    SECRET_KEY: str
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # 邮件配置
    SMTP_HOST: str
    SMTP_PORT: int = 587
    SMTP_USER: str
    SMTP_PASSWORD: str

    # 微信公众号配置 (占位)
    WECHAT_APP_ID: Optional[str] = None
    WECHAT_APP_SECRET: Optional[str] = None
    WECHAT_TOKEN: Optional[str] = None  # 微信服务器验证token
    WECHAT_ENCODING_AES_KEY: Optional[str] = None  # 消息加解密密钥
    
    # 微信小程序配置
    WECHAT_MINIPROGRAM_APP_ID: Optional[str] = None  # 小程序AppID
    WECHAT_USE_MINIPROGRAM_JUMP: bool = False  # 是否使用小程序跳转（默认使用网页跳转）
    
    # Telegram Bot配置
    TELEGRAM_BOT_TOKEN: Optional[str] = None  # Telegram Bot Token
    
    # 应用配置
    DEBUG: bool = False

    # 流量监控配置
    ENABLE_TRAFFIC_MONITOR: bool = True  # 启用流量监控
    TRAFFIC_STATS_RETENTION_DAYS: int = 30  # 统计数据保留天数
    LOG_LEVEL: str = "INFO"
    
    # 监控配置
    MONITOR_INTERVAL: int = 30  # 监控间隔（秒）
    MAX_CONCURRENT_MONITORS: int = 10  # 最大并发监控数
    NOTIFICATION_COOLDOWN: int = 300  # 通知冷却时间（秒）
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"


# 创建全局配置实例
settings = Settings()


def get_database_url() -> str:
    """获取同步数据库连接URL"""
    return settings.DATABASE_URL


def get_async_database_url() -> str:
    """获取异步数据库连接URL"""
    return settings.DATABASE_URL.replace("mysql+pymysql://", "mysql+aiomysql://")


def get_redis_url() -> str:
    """获取Redis连接URL"""
    return settings.REDIS_URL


def is_debug_mode() -> bool:
    """是否为调试模式"""
    return settings.DEBUG
